package cn.fu.security.sha;

import java.security.MessageDigest;
import java.security.Security;

import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.digests.SHA224Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

/**
 * @Title: MySHA.java
 * @Package cn.fu.security.SHA
 * @Description: 消息摘要算法（一系列安 全散列算法）
 * @author zhangxiaofu
 * @Company www.viewhigh.com
 * @date 2016年8月21日 上午1:30:06
 * @version V1.0
 */
public class MySHA {
	private static String src = "99fu";

	public static void main(String[] args) {
		bcSHA1();
		bcSHA224();
		bcSHA224_2();
		ccSHA1();
	}

	public static void bcSHA1() {
		Digest digest = new SHA1Digest();
		// 起始位，长度
		digest.update(src.getBytes(), 0, src.getBytes().length);
		byte[] sha1Byte = new byte[digest.getDigestSize()];
		digest.doFinal(sha1Byte, 0);
		System.err.println("bcSHA-1:" + Hex.toHexString(sha1Byte));
	}

	public static void bcSHA224() {
		Digest digest = new SHA224Digest();
		// 起始位，长度
		digest.update(src.getBytes(), 0, src.getBytes().length);
		byte[] sha224Byte = new byte[digest.getDigestSize()];
		digest.doFinal(sha224Byte, 0);
		System.err.println("bcSHA-224:" + Hex.toHexString(sha224Byte));
	}

	// 通过配制
	public static void bcSHA224_2() {
		try {
			Security.addProvider(new BouncyCastleProvider());
			MessageDigest md = MessageDigest.getInstance("SHA224");
			byte[] sha224Byte = md.digest(src.getBytes());
			System.err.println("bcSHA-224_2:" + Hex.toHexString(sha224Byte));
		} catch (Exception e) {
		}
	}

	public static void ccSHA1() {
		System.err.println("ccSHA1:" + DigestUtils.sha1Hex(src.getBytes()));
		System.err.println("ccSHA1_2:" + DigestUtils.sha1Hex(src));
	}

}
